/*	
   ______   ______    
  /\__  __\/\ \___\ 
  \/_/\ \_/\ \ \__/    __   __     __       
     \ \ \  \ \ \__\  /\ \ /\ \  /'__`\  
     _\_\ \  \ \ \_/_ \ \ \\_\ \/\  __/       
    /\____/   \ \_____\\ \_____/\ \____\   
    \_/__/     \/_____/ \/____/  \/____/                   
 
 	This file is part of the JEVE core API.

    JEve is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    JEve is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Copyright 2008 JEve Project (JEveProject@gmail.com)
 */
package be.fomp.jeve.core.config;

import be.fomp.jeve.core.exceptions.JEveConfigurationException;
import be.fomp.jeve.core.exceptions.JEveConfigurationInitializationException;

import java.io.*;
import java.util.Properties;

/**
 * <pre>
 *  ______   ______
 * /\__  __\/\ \___\
 * \/_/\ \_/\ \ \__/    __   __     __
 *    \ \ \  \ \ \__\  /\ \ /\ \  /'__`\
 *    _\_\ \  \ \ \_/_ \ \ \\_\ \/\  __/
 *   /\____/   \ \_____\\ \_____/\ \____\
 *   \_/__/     \/_____/ \/____/  \/____/
 * </pre>
 * This file is part of the JEVE core API.<br />
 * <br />
 * <p/>
 * Class containing configuration data for the EVE-Online API.
 *
 * @author Sven Meys
 */
abstract class AbstractJEveConfiguration implements JEveConfiguration {

	/**
	 * Contains the file location defined in the subclass.
	 */
	private final String CONFIG_FILE_NAME = getConfigFileName();

	/**
	 * Contains the properties inside the configuration file defined by the subclass.
	 */
	private final Properties PROPERTIES = getConfigurationProperties();

	/**
	 * Private constructor. This class is a singleton.
	 * If the configuration file does not exist this method will attempt to create a new one and
	 * initialize it with default values.<br />
	 * Otherwise this method will load the properties from the existing file.
	 * 
	 * @throws JEveConfigurationInitializationException When configuration file creation failed.
	 * @throws JEveConfigurationException When loading the properties of the configuration failed.
	 */
	public AbstractJEveConfiguration() throws JEveConfigurationInitializationException, JEveConfigurationException {

		File configFile = new File(CONFIG_FILE_NAME);
		try {
			if (!configFile.exists()) {
				configFile.createNewFile();
			 }
			loadConfiguration();
		} catch (IOException ioe) {
				throw new JEveConfigurationInitializationException("Unable to create configuration file at: "+ CONFIG_FILE_NAME,ioe);
		}
	}

	/**
	 * This method loads the configuration from the file specified in CONFIG_FILE_NAME.
	 * If the file does not exist an exception is thrown.
	 * @throws JEveConfigurationException When loading the configuration failed
	 * @see CoreConfiguration
	 */
	public final void loadConfiguration() throws JEveConfigurationException {

		InputStream in = null;
		try {
			in = new FileInputStream(CONFIG_FILE_NAME);
			PROPERTIES.load(in);

		} catch (IOException ioe) {
			throw new JEveConfigurationException("Unable to load configuration from:"+CONFIG_FILE_NAME,ioe);
		} finally {
			try {
				if (in != null) {
					in.close();
				}

			} catch (Exception e) {
				System.err.println("Unable to close inputstream: \n"+e.getMessage());
			}
		}
	}

	/**
	 * Stores the current configuration to a file defined in CONFIG_FILE_NAME
	 *
	 * @throws JEveConfigurationException When loading the configuration failed
	 */
	public final void storeConfiguration() throws JEveConfigurationException {

		OutputStream out = null;
		try {
			out = new FileOutputStream(CONFIG_FILE_NAME);
			PROPERTIES.store(out, COMMENT);

		} catch (IOException ioe) {
			throw new JEveConfigurationException("Unable to store configuration in:"+CONFIG_FILE_NAME,ioe);
		} finally {
			try {
				if (out != null) {
					out.close();
				}

			} catch (Exception e) {
				System.err.println("Unable to close outputstream: \n"+e.getMessage());
			}
		}
	}

	/**
	 * Getter for the configuration file location which must be defined in a subclass.
	 *
	 * @return A string containing the location of the configuration file.
	 */
	public abstract String getConfigFileName();

	/**
	 * Getter for properties. This way this class can be accessed in a generic way.
	 *
	 * @return Properties stored in the config file
	 */
	public abstract Properties getConfigurationProperties();
}
